<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/6/12
 * Time: 20:43
 */

namespace App\Helpers;


use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class CreateQrCode
{
    private $QrCode;

    private $QrCodeImg;

    private $QrCodeImgUrl = false;

    private function __construct()
    {


    }

    public static function make()
    {
        return new static();
    }

    /**
     * 生成二维码首先调用次方法
     * @return $this
     */
    public function bindQrCode()
    {
        $this->QrCode = QrCode::format('png')->encoding('UTF-8');
        return $this;
    }

    /**
     * @title 二维码容错率
     * @param string $level L 7%的字节码恢复率. M	15%的字节码恢复率. Q25% 的字节码恢复率.  H 30%的字节码恢复率.
     * @return $this
     */
    public function setCorrection($level= 'L')
    {
        $this->QrCode->errorCorrection($level);
        return $this;
    }

    /**
     * before:createCode
     * @param string $size
     * @return $this
     */
    public function setSize($size = '')
    {
        $this->QrCode->size($size);
        return $this;
    }

    /**
     * before:createCode
     * @param int $margin
     * @return $this
     */
    public function setMargin($margin=0)
    {
        $this->QrCode->margin($margin);
        return $this;
    }

    /**
     * before:createCode
     * @param $logo_path
     * @param float $percentage
     * @param bool $absolute
     * @return $this
     */
    public function mergeLogo($logo_path,$percentage = 0.2, $absolute = true)
    {
        $this->QrCode->merge($logo_path, $percentage,$absolute);
        return $this;
    }

    /**
     * @title 生成二维码
     * @param $content
     * @return $this
     */
    public function createCode($content)
    {
        $this->QrCodeImg =  $this->QrCode->generate($content);
        return $this;
    }

    /**
     * after:createCode
     * @返回base64图片
     * @return string
     */
    public function getQrCode()
    {
        return base64_encode($this->QrCodeImg);
    }


    /**
     * after:createCode
     * @param $name
     * @return $this
     */
    public function storageCode($name)
    {
        $name = $name.'.png';
        if(Storage::disk('qr_code')->put($name, $this->QrCodeImg)){
            $this->QrCodeImgUrl = Storage::disk('qr_code')->url($name);
        }
        return $this;
    }

    /**
     * after:storageCode或直接使用
     * @param $name
     * @return $this
     */
    public function setCodeUrl($name)
    {
        $name = $name.'.png';
        if(Storage::disk('qr_code')->exists($name)){
            $this->QrCodeImgUrl = Storage::disk('qr_code')->url($name);
        }else{
            $this->QrCodeImgUrl = false;
        }
        return $this;
    }

    /**
     * after:setCodeUrl
     * @return bool
     */
    public function getCodeUrl()
    {
        return $this->QrCodeImgUrl;
    }

    /**
     * @param $bgImg
     * @param $save_path
     * top-left (default) top  top-right left center  right bottom-left bottom bottom-right
     * @param int $x
     * @param int $y
     * @return mixed
     */
    public function mergeBackgroundImg($bgImg,$save_path,$x=0,$y=0)
    {
        $img = Image::make($bgImg);
        return $img->insert($this->QrCodeImgUrl,'top-left',$x,$y)->save($save_path);
    }
}